package cn.hzking.gjmall.activitys.second;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.LinearLayout;

import com.just.agentwebX5.AgentWebX5;
import com.just.agentwebX5.ChromeClientCallbackManager;
import com.just.agentwebX5.DefaultWebClient;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.app.Gjmall;
import cn.hzking.gjmall.base.ColorStatuBarActivity;
import cn.hzking.gjmall.view.SimpleOptionView;
import cn.hzking.gjmall.webview.GjWebInterface;

/**
 * 商品活动页面
 */
public class GoodActiivtyActivity extends ColorStatuBarActivity {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.web_ll)
    LinearLayout webLl;
    private AgentWebX5 mAgentWebX5;
    private String url = "http://gjmall.mxth.com/shop/native/ind.htm?shopid=1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_good_actiivty);
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        webLl = findViewById(R.id.web_ll);
        initWebView();
    }

    private void initWebView() {
        mAgentWebX5 = AgentWebX5.with(this)//
                .setAgentWebParent(webLl, new LinearLayout.LayoutParams(-1, -1))//
                .useDefaultIndicator()//
                .defaultProgressBarColor()
                .setReceivedTitleCallback(mCallback)
                .setWebChromeClient(mWebChromeClient)
                .setWebViewClient(mWebViewClient)
                .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.DISALLOW)
                .interceptUnkownScheme()
                .setSecutityType(AgentWebX5.SecurityType.strict)
               // .addJavascriptInterface(Gjmall.getJsInterface(), GjWebInterface.create(this))
                .createAgentWeb()//
                .ready()
                .go(null);
        mAgentWebX5.getJsInterfaceHolder().addJavaObject(Gjmall.getJsInterface(), GjWebInterface.create(this));
        mAgentWebX5.getWebCreator().get().getX5WebViewExtension().setScrollBarFadingEnabled(false);
        mAgentWebX5.getLoader().loadUrl(url);
    }

    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String s) {
            Intent intent = new Intent(GoodActiivtyActivity.this,GoodActiivtyActivity.class);
            intent.putExtra("url",s);
            GoodActiivtyActivity.this.startActivity(intent);
            return true;
        }
    };

    private WebChromeClient mWebChromeClient = new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView webView, int i) {
            super.onProgressChanged(webView, i);

        }
    };
    private ChromeClientCallbackManager.ReceivedTitleCallback mCallback = new ChromeClientCallbackManager.ReceivedTitleCallback() {
        @Override
        public void onReceivedTitle(WebView view, String title) {
            if (simpleTileView !=null)
            simpleTileView.setTitle(title);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
    @Override
    protected void onPause() {
        mAgentWebX5.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgentWebX5.getWebLifeCycle().onResume();
        super.onResume();
    }
}
